package com.xhwl.centiotdata.integrator;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.integration.vehicleschedule.VehicleScheduleIntegrationConfig;
import com.xhwl.centiotdata.pojo.constant.YaoAnConstant;
import com.xhwl.centiotdata.utils.HttpUtils;
import com.xhwl.centiotdata.utils.LocalDateTimeUtils;
import com.xhwl.common.pojo.cent.government.EmployeeVRExperienceDetail;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/10/4 17:03
 * Description: 中交兴路 集成处理器
 */
public class SinoiovIntegrationHandler {

    public SinoiovIntegrationHandler() {
        logger = LoggerFactory.getLogger(getClass().getName());
    }

    protected Logger logger;

    private static final int SUCCESS_CODE = 200;

    public JSONObject executeVSS(VehicleScheduleIntegrationConfig config, String path, JSONObject params) {
        HttpRequest request = HttpUtil.createPost(config.getVssHost() + path);
        if (StringUtils.isNotEmpty(config.getApp())) {
            request.header("appId", config.getApp());
        }
        if (StringUtils.isNotEmpty(config.getAk())) {
            request.header("appKey", config.getAk());
        }
        logger.info("请求配置：{}，请求路径：{}，请求参数：{}", config, config.getTmsHost()+path, params);
        HttpResponse response = request.body(params.toJSONString()).execute();
        if (response == null || response.getStatus() != 200) {
            return new JSONObject();
        }
        String body = response.body();
        logger.info("【请求中交兴路 {}】 结果：{}", path, body);
        return JSON.parseObject(body);

    }

    /**
     * 请求车辆调度test环境，即同时生产和测试同时使用
     */
    public JSONObject executeVSSTest(VehicleScheduleIntegrationConfig config, String path, JSONObject params){
        HttpRequest request = HttpUtil.createPost(config.getVssHostTest() + path);
        if (StringUtils.isNotEmpty(config.getAppTest())) {
            request.header("appId", config.getAppTest());
        }
        if (StringUtils.isNotEmpty(config.getAkTest())) {
            request.header("appKey", config.getAkTest());
        }
        logger.info("请求配置：{}，请求路径：{}，请求参数：{}", config, config.getTmsHost()+path, params);
        HttpResponse response = request.body(params.toJSONString()).execute();
        if (response == null || response.getStatus() != 200) {
            return new JSONObject();
        }
        String body = response.body();
        logger.info("【请求中交兴路测试环境 {}】 结果：{}", path, body);
        return JSON.parseObject(body);
    }

    public JSONObject executeTMS(VehicleScheduleIntegrationConfig config, String path, JSONObject params) {
        HttpRequest request = HttpUtil.createPost(config.getTmsHost() + path);
        if (StringUtils.isNotEmpty(config.getApp())) {
            request.header("appId", config.getApp());
        }
        if (StringUtils.isNotEmpty(config.getAk())) {
            request.header("appKey", config.getAk());
        }
        logger.info("请求配置：{}，请求路径：{}，请求参数：{}", config, config.getTmsHost()+path, params);
        HttpResponse response = request.body(params.toJSONString()).execute();
        if (response == null || response.getStatus() != 200) {
            return new JSONObject();
        }
        String body = response.body();
        logger.info("【请求中交兴路 {}】 结果：{}", path, body);
        return JSON.parseObject(body);
    }

    public JSONObject executeVssForGet(VehicleScheduleIntegrationConfig config, String path, JSONObject params) {
        String url = config.getVssHost() + path;
        HttpRequest request = HttpUtil.createGet(url);
        if (StringUtils.isNotEmpty(config.getApp())) {
            request.header("appId", config.getApp());
        }
        if (StringUtils.isNotEmpty(config.getAk())) {
            request.header("appKey", config.getAk());
        }
        logger.info("请求配置：{}，请求路径：{}，请求参数：{}", config, config.getTmsHost()+path, params);
        request.form(params);
        String result = request.execute().body();
        logger.info("【请求中交兴路 {}】 结果：{}", url, result);

        return JSONObject.parseObject(result);
    }

    public JSONObject executeWMS(VehicleScheduleIntegrationConfig config, String path, JSONObject params) {
        HttpRequest request = HttpUtil.createPost(config.getWmsHost() + path);
        if (StringUtils.isNotEmpty(config.getApp())) {
            request.header("appId", config.getApp());
        }
        if (StringUtils.isNotEmpty(config.getAk())) {
            request.header("appKey", config.getAk());
        }

        HttpResponse response = request.body(params.toJSONString()).execute();
        if (response == null || response.getStatus() != 200) {
            return new JSONObject();
        }
        String body = response.body();
        logger.info("【请求中交兴路 {}】 结果：{}", path, body);
        return JSON.parseObject(body);

    }
}
